<?php
class Admin extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->helper(array('url','form','security'));
	}
	
	//Funktion som sköter inloggningen av admin användare.
	function index()
	{		
		//Kontrollerar så att det är en admin som är inlo
		if ($this->session->userdata('admin') != TRUE)
		{

			/*Validerar användarnamenet och lösenordet*/
			$this->form_validation->set_rules('username','Användarnamn','required|trim|max_length[20]|xss_clean');
			$this->form_validation->set_rules('password','Lösenord','required|trim|max_length[40]|xss_clean');
				
			if ($this->form_validation->run()==FALSE)
			{
				//om det inte finns något att validera kommer detta att köras.			
				$data['title']='Admin login';
				$this->load->view('global/view_master_header',$data);
				$this->load->view('admin/view_admin_login');
				$this->load->view('global/view_master_footer');
			}
			else
			{
				
				$username=$this->input->post('username');
				$password=$this->input->post('password');
				
				//Kollar användaruppgifterna mot databasen
				$this->load->model('admin_model');
				$adminData=$this->admin_model->loginAdmin($username,$password);
				
				if (! $adminData)
				{
					//det gick inte att loggain
					$this->session->set_flashdata('login_error',TRUE);
					redirect('admin/admin');
				
				}
				else
				{
					//det gick att loggain.
					$logInData=array('LoggedIn'=>TRUE,
									'UserId'=>$adminData->ID,								
									'admin'=>TRUE);
								
					
					$this->session->set_userdata($logInData);
	
					
					redirect('admin/admin/menu');
					
				}		
			}
		}
		
		else
		{
			//kod som körs om admin redan är inloggad.
			$data['title']='Logga in admin';
			$data['message']='Redan inloggad som admin'; 
			$this->load->view('global/view_master_header',$data);
			$this->load->view('user/view_result', $data);
			$this->load->view('global/view_master_footer');	
		}
		
	}
	
	
	//funktion som visar huvud menyn för admin användare.
	function menu()
	{
		
		$admin = $this->session->userdata('admin');
		
		//kontrollerar att det är en admin som är inloggad, annars visas ett 404 fel.
		if($admin == TRUE)
		{
					$data['title']='Admin menu';
					$this->load->view('global/view_master_header',$data);
					$this->load->view('admin/view_admin_menu');
					$this->load->view('global/view_master_footer');
		
		}
		else
		{
			show_404();
		}
	}
	
	//Visar alla användare för redigering
	function users()
	{
		
		$admin = $this->session->userdata('admin');
		//kontrollerar att det är en admin som är inloggad. annars visas fel 404
		if($admin == TRUE)
		{
			
		
			$this->load->model('admin_model');
			$users = $this->admin_model->getAllUsers();
		
		
			$data['users'] = $users;
			$data['title']='Annvändare';
			$this->load->view('global/view_master_header',$data);
			$this->load->view('admin/view_admin_users',$data);
			$this->load->view('global/view_master_footer');
			
		}
		else
		{
			show_404();
		}
	
	}				
	
	
	//Visar användar upppgifter för redigering.
	function getUserInfo($id)
	{
		
	
		$admin = $this->session->userdata('admin');
		//Kontrollerar så att admin är inloggad annars visas 404fel
		if($admin == TRUE)
		{
			if(isset($_POST['btnChange']))
			//Kod som körs om användarinformation ska ändras.
			{
					$this->load->model('userinformation_model');
				$userInformation = $this->userinformation_model->getUserInformation($id);
				
				if($userInformation != NULL)
				{
				
				
					$data['userInformation'] = $userInformation;
					$data['title']='Annvändare';
					$data['online']='';
					$data['label']='Kontots info';
					$data['ID']= $id;
					$this->load->view('global/view_master_header',$data);
					$this->load->view('admin/view_admin_userinformation',$data);
					$this->load->view('global/view_master_footer');
				}
				
				else
				{
					echo "Hittade inte informationen";
				}
			}
			
			if(isset($_POST['btnChangePassword']))
			//kod som körs om lösenordet ska ändras.
			{
					$data['ID']= $id;
					$data['title']='Ändra annvändarens lösenord';
					$this->load->view('global/view_master_header',$data);
					$this->load->view('admin/view_admin_changeUserPassword',$data);
					$this->load->view('global/view_master_footer');
			
			}
		}
		else
		{
			show_404();
		}
	}
	
	//Kontrollerar om användarinformation vart ändrad och sparar ny data.
	function checkIfInfoChanged($userID)
	{
		$admin = $this->session->userdata('admin');
		//kontrollerar att det är en admin som är inloggad, annatrs visas 404 fel.	
		if($admin == TRUE)
		{
				
			$this->load->model('userinformation_model');
			$oldInfo = $this->userinformation_model->getUserInformation($userID);
		
					
			$newFirstName=trim($this->input->post('fname'));
			$newLastName=trim($this->input->post('lname'));
			$newPersonNumber=trim($this->input->post('pnum'));
			$newAdress=trim($this->input->post('adress'));
			$newTelNumber=trim($this->input->post('telNumber'));
			$newEmail=trim($this->input->post('email'));
			
			$dataChanged = FALSE;
			
			
			if ($newFirstName != $oldInfo->Fnamn)
			{
				$result = $this->userinformation_model->saveNewFname($userID, $newFirstName);
				
				$dataChanged = TRUE;
			}
			
			if ($newLastName != $oldInfo->Enamn)
			{
				$result = $this->userinformation_model->saveNewLname($userID, $newLastName);
				
				$dataChanged = TRUE;
			}
			
			if ($newPersonNumber != $oldInfo->Personnummer)
			{
				$result = $this->userinformation_model->saveNewPnumber($userID, $newPersonNumber);
				
				$dataChanged = TRUE;
			}
			
			if ($newAdress != $oldInfo->Adress)
			{
				$result = $this->userinformation_model->saveNewAdress($userID, $newAdress);
				
				$dataChanged = TRUE;
			}
			
			if ($newTelNumber != $oldInfo->TelNummer)
			{
				$result = $this->userinformation_model->saveNewTelNumber($userID, $newTelNumber);
				
				$dataChanged = TRUE;
			}
			
			if ($newEmail != $oldInfo->Email)
			{
				$result = $this->userinformation_model->saveNewEmail($userID, $newEmail);
				
				$dataChanged = TRUE;
			}
			
			
			if($dataChanged == TRUE)
			{
					
					$data['title']='Ändrad info';
					$data['message']='Nya informationen är sparad'; 
					$this->load->view('global/view_master_header',$data);
					$this->load->view('user/view_result', $data);
					$this->load->view('global/view_master_footer');	
					
			}
				
			else
			{
					$data['title']='Ändrad info';
					$data['message']='Ingen ny information sparad'; 
					$this->load->view('global/view_master_header',$data);
					$this->load->view('user/view_result', $data);
					$this->load->view('global/view_master_footer');	
			}		
		
		}
		else
		{
			show_404();
		}
	
	}
	
	//funktion för att söka efter en speciell användare.
	function searchUsers()
	{
		$admin = $this->session->userdata('admin');
		//Kontrollerar att det är en admin som är inloggad annars visas ett 404 fel.
		if($admin == TRUE)
		{
				
			$email=trim($this->input->post('mail'));
			
			$this->load->model('admin_model');
			$users = $this->admin_model->getSearchUser($email);
			
			$data['users'] = $users;
			$data['title']='Annvändare';
			$this->load->view('global/view_master_header',$data);
			$this->load->view('admin/view_admin_users',$data);
			$this->load->view('global/view_master_footer');
				
		}
		else
		{
			show_404();
		}
	}
	
	//funktion för att ändra lösenord ppå en användare.	
	function changePassword()
	{
		$admin = $this->session->userdata('admin');
		//kontrollerar att det är en admin som är inloggad, annars visas 404 fel.
		if($admin == TRUE)
		{
	
			$newPassword=trim($this->input->post('newPassword'));
			$userID=trim($this->input->post('txtID'));
			$newPasswordConfirm=trim($this->input->post('newPasswordConfirm'));
			
			
			
		
		
	
			if ($newPassword == $newPasswordConfirm)
			{
				$this->load->model('userinformation_model');
				$result = $this->userinformation_model->saveNewPassword($userID, $newPassword);
			
				if($result == 1)
				{
						$data['title']='Nytt lösenord';
						$data['message']='Nya lösenordet är sparat!'; 
						$this->load->view('global/view_master_header',$data);
						$this->load->view('user/view_result', $data);
						$this->load->view('global/view_master_footer');	
				}
				
				else
				
				{
						$data['title']='Nytt lösenord';
						$data['message']='Nått gick fel, lösenordet blev inte sparat!'; 
						$this->load->view('global/view_master_header',$data);
						$this->load->view('user/view_result', $data);
						$this->load->view('global/view_master_footer');
				}
			}
			else
			{
						$data['title']='Nytt lösenord';
						$data['message']='Nått gick fel, lösenordet blev inte sparat!'; 
						$this->load->view('global/view_master_header',$data);
						$this->load->view('user/view_result', $data);
						$this->load->view('global/view_master_footer');
			}
		}
		
		else
		{
			show_404();
		}
	}

	
	//funktion för att visa alla bokningar.
	function bookings()
	{

		$admin = $this->session->userdata('admin');
		//kontrollerar att det är en admin som är inloggad.
		if($admin == TRUE)
		{
			$this->load->model('admin_model');
			$bookings = $this->admin_model->getAllBookings();
			
			$this->load->model('userinformation_model');
		
			
			foreach($bookings as & $row)
			{
				$user = $this->userinformation_model->getUserInformation($row['KundID']);
				$row['KundID'] = $user->Email;
			}
			
		
				$data['bookings'] = $bookings;
				$data['title']='Bokningar';
				$this->load->view('global/view_master_header',$data);
				$this->load->view('admin/view_admin_bookings',$data);
				$this->load->view('global/view_master_footer');
			
		}
		else
		{
			show_404();
		}
		
	}
	//Funktion för att söka efter bokningar medhjälp av användarens email.
	function searchBookings()
	{
	
		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{	
			$email=trim($this->input->post('mail'));
			$this->load->model('admin_model');
			$this->load->model('userinformation_model');
			
			$bookings = $this->admin_model->searchBookings($email);
			
			
			
			
			
			
				
					// om det är några bokningar för vald email.
					if($bookings != "")
					{
						
						
						foreach($bookings as & $row)
						{
							$user = $this->userinformation_model->getUserInformation($row['KundID']);
							$row['KundID'] = $user->Email;
						}
								
						
						
						$data['bookings'] = $bookings;
						$data['title']='Bokningar';
						$this->load->view('global/view_master_header',$data);
						$this->load->view('admin/view_admin_bookings',$data);
						$this->load->view('global/view_master_footer');
							
					}
					else
					{
						$data['title']='Bokningar';
						$data['message'] = "Inga bokningar med den emailen";
						$this->load->view('global/view_master_header',$data);
						$this->load->view('admin/view_admin_message',$data);
						$this->load->view('global/view_master_footer');
					}
					
		}
		else
		{
			show_404();
		}		
		
	}
	
	//funktion för att visa alla bilar.
	function cars()
	{
		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
		
			$this->load->model('admin_model');
			$theCars = $this->admin_model->getAllCars();
		
		
			
			$data['cars'] = $theCars;
			$data['title']='Bilarna';
			$this->load->view('global/view_master_header',$data);
			$this->load->view('admin/view_admin_cars',$data);
			$this->load->view('global/view_master_footer');

		}
		else
		{
			show_404();
		}
	}
	
	//HÄmtar information om en bil.
	function getCarInfo()
	{
		
		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
			$ID=$this->input->post('txtID');
			$this->load->model('admin_model');
			
			$theCar = $this->admin_model->getACar($ID);
			
			
			
			
			$data['car'] = $theCar;
			$data['title']='Information om bilen';
			$this->load->view('global/view_master_header',$data);
			$this->load->view('admin/view_admin_acar',$data);
			$this->load->view('global/view_master_footer');
		}
		else
		{
			show_404();
		}
	}
	
	//kontrollerar ny bil information och sparar.
	function checkNewCarInfo()
	{
		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
				
			$this->load->model('admin_cars_model');
			$this->load->model('admin_model');
			
			$ID = $this->input->post('txtID');
			
			$oldInfo = $this->admin_model->getACar($ID);
			
			
			$newRegPlate=trim($this->input->post('regPlate'));
			$newSeats=trim($this->input->post('seats'));
			$newModel=trim($this->input->post('model'));
			$newDisc=trim($this->input->post('disc'));
			$newSizeClass=trim($this->input->post('sizeClass'));
			$newKeyNumber=trim($this->input->post('keyNumber'));
			
			
			
			
			$dataChanged = FALSE;
			
			
		
			if ($newRegPlate != $oldInfo->RegPlåt)
			{
				$result = $this->admin_cars_model->saveNewRegPlate($ID, $newRegPlate);
				
				$dataChanged = TRUE;
			}
			
			if ($newSeats != $oldInfo->Sittplatser)
			{
				$result = $this->admin_cars_model->saveNewSeats($ID, $newSeats);
				
				$dataChanged = TRUE;
			}
			
			if ($newModel != $oldInfo->Modell)
			{
				$result = $this->admin_cars_model->saveNewModel($ID, $newModel);
				
				$dataChanged = TRUE;
			}
			
			if ($newDisc != $oldInfo->Info)
			{
				$result = $this->admin_cars_model->saveNewDisc($ID, $newDisc);
				
				$dataChanged = TRUE;
			}
			
			if ($newSizeClass != $oldInfo->Storleksklass)
			{
				$result = $this->admin_cars_model->saveNewSizeClass($ID, $newSizeClass);
				
				$dataChanged = TRUE;
			}
		
			if ($newKeyNumber != $oldInfo->NyckelNR)
			{
				$result = $this->admin_cars_model->saveNewKeyNumber($ID, $newKeyNumber);
				
				$dataChanged = TRUE;
			}
		
			
			
			
			if($dataChanged == TRUE)
			{
					
					$data['title']='Ändrad info';
					$data['message']='Nya bil informationen är sparad'; 
					$this->load->view('global/view_master_header',$data);
					$this->load->view('user/view_result', $data);
					$this->load->view('global/view_master_footer');	
					
			}
				
			else
			{
					$data['title']='Ändrad info';
					$data['message']='Ingen ny bil information sparad'; 
					$this->load->view('global/view_master_header',$data);
					$this->load->view('user/view_result', $data);
					$this->load->view('global/view_master_footer');	
			}
			
		}
		else
		{
			show_404();
		}
	}
	
	//funktion som visar viewn för att registrera en ny bil.
	function newcar()
	{
	
		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
					$data['title']='Lägg till en ny bil';
					
					$this->load->view('global/view_master_header',$data);
					$this->load->view('admin/view_admin_register_acar', $data);
					$this->load->view('global/view_master_footer');
	
		}
		else
		{
			show_404();
		}
					
	}
	
	
	
	//funktion för att registrera en ny bil.
	function registerCar()
	{
		
			$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
			//Kod för att ladda upp en bild//
			$config['upload_path'] = 'C:/Server/wamp/www/studentbilar/bil_bilder/upload';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '100';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';
			
			$this->load->library('upload', $config);
		
			if ( ! $this->upload->do_upload())
			{
				//Kod som körs om inte bilden går att ladda upp.
				$data['error'] = array('error' => $this->upload->display_errors());
	
				$data['title']='Lägg till en ny bil';	
				$this->load->view('global/view_master_header',$data);
				$this->load->view('admin/view_admin_register_acar', $data);
				$this->load->view('global/view_master_footer');
			}
			else
			{
				//Kod som körs om bilden gick att ladda upp.
				$data=array('upload_data' => $this->upload->data());
				
				if(!$this->_manipulatePicture($data['upload_data']['full_path']))
				{
						//Kod som körs om det inte gick att skapa en thumbnail.
					$data['title']='Lägg till en ny bil';
					$data['error']='Det gick inte att skapa upp en thumbnail av bilden';	
					$this->load->view('global/view_master_header',$data);
					$this->load->view('admin/view_admin_register_acar', $data);
					$this->load->view('global/view_master_footer');
				}
				else
				{
					//kod som körs om det gick att skapa en thumbnail
					$picturePath='bil_bilder/small/'.$data['upload_data']['raw_name'].'_thumb'.$data['upload_data']['file_ext'];
					$RegPlate=trim($this->input->post('regPlate'));
					$Seats=trim($this->input->post('seats'));
					$Model=trim($this->input->post('model'));
					$Disc=trim($this->input->post('disc'));
					$SizeClass=trim($this->input->post('sizeClass'));
					$KeyNumber=trim($this->input->post('keyNumber'));
			
					$regPlateLen = strlen($RegPlate);
			
					if ($RegPlate != '' && $regPlateLen == 6 && $Seats != '' &&  $Model != '' &&  $Disc != '' &&  $SizeClass != '' &&  $KeyNumber != '')
					{
						$this->load->model('admin_cars_model');
						$result = $this->admin_cars_model->RegisterCar($RegPlate, $Seats, $Model, $Disc, $SizeClass, $picturePath,'storbild', $KeyNumber);
				
						if($result == 1)
						{
							$data['title']='Registrera bil';
							$data['message']='Bilen är registrerad i databasen'; 
							$this->load->view('global/view_master_header',$data);
							$this->load->view('user/view_result', $data);
							$this->load->view('global/view_master_footer');	
						}
				
						else
						{
							$data['title']='Registrera bil';
							$data['message']='Nått gick fel med registreringen'; 
							$this->load->view('global/view_master_header',$data);
							$this->load->view('user/view_result', $data);
							$this->load->view('global/view_master_footer');	
						}	
					}
			
					else
					{
						$data['title']='Registrera bil';
						$data['message']='Nån av raderna var lämnade tomma eller så var regplåten inte 6 tecken lång'; 
						$this->load->view('global/view_master_header',$data);
						$this->load->view('user/view_result', $data);
						$this->load->view('global/view_master_footer');
					}
	
				}
			}
		}
		else
		{
			show_404();
		}
	}
	
	private function _manipulatePicture($fullPath)
	{
		//Gör en thumbnail  av bilden
		$config['image_library'] = 'gd2';
		$config['source_image']	= $fullPath;
		$config['new_image']='C:/Server/wamp/www/studentbilar/bil_bilder/small/';
		$config['create_thumb'] = TRUE;
		$config['maintain_ratio'] = TRUE;
		$config['width']= 91;
		$config['height']= 68;
		
		$this->load->library('image_lib',$config);
		$this->image_lib->resize();
		
		if($this->image_lib->resize())
		{
			//Kod som körs om det gick bra.
			return TRUE;	
		}
		else
		{
			//Kod som körs om det inte gick.
			return FALSE;
		}
		
	}
		
		
	//Visar alla prisklasser	
	function prices()
	{
		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
				$this->load->model('admin_model');
				$priceClasses = $this->admin_model->getAllPriceClasses();	
						
				$data['title']='Lägg till en ny bil';
				$data['priceClasses']=$priceClasses;		
				$this->load->view('global/view_master_header',$data);
				$this->load->view('admin/view_admin_priceClasses', $data);
				$this->load->view('global/view_master_footer');
			
		}
		else
		{
			show_404();
		}
			
	}	
		
	//Visar information om en specefik prisklass.
	function priceclass()
	{	
		$admin = $this->session->userdata('admin');
		if($admin == TRUE)
		{
			
			$priceClass=trim($this->input->post('txtID'));
			
			$this->load->model('admin_model');
			
		 	$prices = $this->admin_model->getPriceClassDetails($priceClass);
			
			
			
				$data['title']='Priklass';
				
				$data['prices']= $prices;	
				$this->load->view('global/view_master_header',$data);
				$this->load->view('admin/view_admin_onePriceClass', $data);
				$this->load->view('global/view_master_footer');
		}
		else
		{
			show_404();
		}
	}
	
	//Visar priset för en specefik bil i en prisklass.	
	function carPrices()
	{
	
		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
			$carID=$this->input->post('txtID');
			$priceClassID=$this->input->post('txtClassID');
			
			$this->load->model('admin_cars_model');
			$carPrices = $this->admin_cars_model->getCarPrices($carID, $priceClassID);
			
			
			
				$data['title']='Priserna för bilen';
				$data['carprices']= $carPrices;	
				$this->load->view('global/view_master_header',$data);
				$this->load->view('admin/view_admin_aCarPrices', $data);
				$this->load->view('global/view_master_footer');
		}
		else
		{
			show_404();
		}
		
	
	}
		
	//Sparar bilpris.
	function saveCarPrices()
	{

		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
			$carID=$this->input->post('txtID');
			$priceClassID=$this->input->post('txtClassID');
			
			$newWeekPrice=$this->input->post('weekPrice');
			$newDayPrice=$this->input->post('dayPrice');
			$newHourPrice=$this->input->post('hourPrice');
			
			$this->load->model('admin_cars_model');
			
			$changed = FALSE;
			
			if($newWeekPrice != '' && $this->int($newWeekPrice) == 1)
			{
				$result = $this->admin_cars_model->saveCarWeekPrice($carID, $priceClassID, $newWeekPrice);
				if($result == 1)
				{
					$changed = TRUE;
				}
			}
			
			if($newDayPrice != '' && $this->int($newDayPrice) == 1)
			{
				$result = $this->admin_cars_model->saveCarDayPrice($carID, $priceClassID, $newDayPrice);
				if($result == 1)
				{
					$changed = TRUE;
				}
			}
			
			if($newHourPrice != '' && $this->int($newHourPrice) == 1)
			{
				$result = $this->admin_cars_model->saveCarHourPrice($carID, $priceClassID, $newHourPrice);
				if($result == 1)
				{
					$changed = TRUE;
				}
			}
			
			
			
			if($changed == TRUE)
			{
						$data['title']='Ändrade priser';
						$data['message']='De nya priserna är sparade'; 
						$this->load->view('global/view_master_header',$data);
						$this->load->view('user/view_result', $data);
						$this->load->view('global/view_master_footer');	
			}
			
			else
			{
						$data['title']='Ändrade priser';
						$data['message']='Inga nya priser för bilen är sparade'; 
						$this->load->view('global/view_master_header',$data);
						$this->load->view('user/view_result', $data);
						$this->load->view('global/view_master_footer');	
			}
		}
		else
		{
			show_404();
		}
	}
	
	
	
	//sparar ny prisklass information.
	function savePriceClassInfo()
	{
		$admin = $this->session->userdata('admin');
		if($admin == TRUE)
		{
			$priceClassID=$this->input->post('txtClassID');
			$newName=$this->input->post('priceClassName');
			$newInfo=$this->input->post('info');
			
			$this->load->model('admin_model');
			
			$changed = FALSE;
			
			if($newName != '')
			{
				$result = $this->admin_model->savePriceClassName($priceClassID, $newName);
				
				if($result == 1)
				{
					$changed = TRUE;
				}
			}
			
			
			if($newInfo != '')
			{
				$result = $this->admin_model->savePriceClassInfo($priceClassID, $newInfo);
				
				if($result == 1)
				{
					$changed = TRUE;
				}
			}
			
			
			
			if($changed == TRUE)
			{
						$data['title']='Prisklassinformation';
						$data['message']='Den nya prisklassinformationen är sparad'; 
						$this->load->view('global/view_master_header',$data);
						$this->load->view('user/view_result', $data);
						$this->load->view('global/view_master_footer');	
			}	
			
			
			else
			{
						$data['title']='Prisklassinformation';
						$data['message']='Ingen ny prisklassinformation sparad'; 
						$this->load->view('global/view_master_header',$data);
						$this->load->view('user/view_result', $data);
						$this->load->view('global/view_master_footer');	
			}
		}
		else
		{
			show_404();
		}
	}
	
	//visar view för att skapa ny prisklass
	function newPriceClass()
	{
		$admin = $this->session->userdata('admin');
			
		if($admin == TRUE)
		{
			$this->load->model('admin_model');
				
			$cars = $this->admin_model->getAllCars();
			$carRows = count($cars);
			
			
			$data['title']='Ny prisklass';
			$data['carRows'] =$carRows;
			$data['cars']=$cars;
			$this->load->view('global/view_master_header',$data);
			$this->load->view('admin/view_admin_register_aPriceClass', $data);
			$this->load->view('global/view_master_footer');
	
		}
		else
		{
			show_404();
		}
	}
	
	//Funktion för att spara  en ny prisklass.
	function saveNewPriceClass()
	{

		$admin = $this->session->userdata('admin');
			
			if($admin == TRUE)
			{
				$priceClassName=trim($this->input->post('priceClassName'));
				$info= $this->input->post('info');
				
				$this->load->model('admin_cars_model');
				
				$x = $this->input->post('txtCarRows');
				
				//Controller int if all prices was saved in the database
				$y = 0;
				
				
				$result = $this->admin_cars_model->insertPriceClass($priceClassName, $info);
				
				if($result != '')
				{
					
					
					//a loop that saves the new prices for each car, number of rows in the car table was saved in a hidden field on the view.
					for($i = 0 ; $i <= $x  ; $i++)
					{
						$carID= $this->input->post($i . 'txtID');
						$weekPrice = $this->input->post( $i . 'weekprice');
						$dayPrice = $this->input->post( $i . 'dayprice');
						$hourPrice = $this->input->post( $i . 'hourprice');
					
						if ($weekPrice != '' && $this->int($weekPrice) == 1)
						{
							if ($dayPrice!= '' && $this->int($dayPrice) == 1)
							{
								if($hourPrice != '' && $this->int($hourPrice) == 1)
								{
									
									$priceClass = $this->admin_cars_model->getPriceClassID($priceClassName);
									$priceClassID = $priceClass->ID;
									
									$result = $this->admin_cars_model->insertCarPrices($carID, $priceClassID, $weekPrice, $dayPrice, $hourPrice);
									
									if($result == 1)
									{
										$y++;
									}
									
									
									
								}
								
								
								
							}	   
							   
							   
						}
					
					
					}
					
					if($y == $x)
					{
						echo "lyckad reg!";
						
					}
					
					else
					{
						echo "nått gick fel i registeringen av priserna på nån bilarna";
					}
				}
				
				else 
				{
					echo "nått gick fel med registeringen av prisklassen";
				}
				
		}
		else
		{
			show_404();
		}
	}
	
	//Visar view för att skapa en ny discount
	function newdiscounts()
	{
		$admin = $this->session->userdata('admin');
			
			if($admin == TRUE)
			{
							$data['title']='Skapa nya rabatter';
							$data['errorOneUse'] ='';
							$data['errorPeriod']='';
							$this->load->view('global/view_master_header',$data);
							$this->load->view('admin/view_admin_register_discounts', $data);
							$this->load->view('global/view_master_footer');	
							
			}
			else
			{
				show_404();
			}
	}


	//sparar en engåmngsrabatt	
	function saveOneUseDiscount()
	{
		$admin = $this->session->userdata('admin');
			
			if($admin == TRUE)
			{
				$this->load->model('admin_model');
				
				$code = $this->input->post('discountCode');
				$discount = $this->input->post('discount');
				
				if($code != '')
				{
				
					if($discount != '')
					{
						$result = $this->admin_model->insertOneUseDiscount($code, $discount);
						
						if($result== 1)
						{
									$data['title']='Prisklassinformation';
									$data['message']='Den nya rabatten är sparad'; 
									$this->load->view('global/view_master_header',$data);
									$this->load->view('user/view_result', $data);
									$this->load->view('global/view_master_footer');	
						}
					}
					
					else
					{
									$data['title']='Skapa nya rabatter';
									$data['errorOneUse'] ='<b><font color=red>Ingen rabatt inskriven!</font></b>';
									$data['errorPeriod']='';
									$this->load->view('global/view_master_header',$data);
									$this->load->view('admin/view_admin_register_discounts', $data);
									$this->load->view('global/view_master_footer');	
					}
					
				}
				
				else
				{
									$data['title']='Skapa nya rabatter';
									$data['errorOneUse'] ='<b><font color=red>Ingen kod inskriven!</font></b>';
									$data['errorPeriod']='';
									$this->load->view('global/view_master_header',$data);
									$this->load->view('admin/view_admin_register_discounts', $data);
									$this->load->view('global/view_master_footer');	
				}
		}
	}
	
	//spara period rabatt
	function savePeriodDiscount()
	{
		$admin = $this->session->userdata('admin');
		
		if($admin == TRUE)
		{
			$this->load->model('admin_model');
			
			$code = $this->input->post('discountCode');
			$discount = $this->input->post('discount');
			$startDate = $this->input->post('startDate');
			$endDate = $this->input->post('endDate');
			
				if($code != '')
			{
			
				if($discount != '')
				{
					if($startDate != '' && $endDate != '')
					{
						$result = $this->admin_model->insertPeriodDiscount($code, $discount, $startDate, $endDate);
						if($result== 1)
						{
									$data['title']='Prisklassinformation';
									$data['message']='Den nya rabatten är sparad'; 
									$this->load->view('global/view_master_header',$data);
									$this->load->view('user/view_result', $data);
									$this->load->view('global/view_master_footer');	
						}
					}	
					
					else
					{
								$data['title']='Skapa nya rabatter';
								$data['errorOneUse'] ='';
								$data['errorPeriod']='<b><font color=red>Saknades ett datum!</font></b>';
								$this->load->view('global/view_master_header',$data);
								$this->load->view('admin/view_admin_register_discounts', $data);
								$this->load->view('global/view_master_footer');	
					}
					
				}
				
				else
				{
								$data['title']='Skapa nya rabatter';
								$data['errorOneUse'] ='';
								$data['errorPeriod']='<b><font color=red>Ingen rabatt inskriven!</font></b>';
								$this->load->view('global/view_master_header',$data);
								$this->load->view('admin/view_admin_register_discounts', $data);
								$this->load->view('global/view_master_footer');	
				}
				
			}
			
			else
			{
								$data['title']='Skapa nya rabatter';
								$data['errorOneUse'] ='';
								$data['errorPeriod']='<b><font color=red>Ingen kod inskriven!</font></b>';
								$this->load->view('global/view_master_header',$data);
								$this->load->view('admin/view_admin_register_discounts', $data);
								$this->load->view('global/view_master_footer');	
			}
		}
		else
		{
			show_404();
		}
	}


	//visar rabatter
	function discounts()
	{	
		$admin = $this->session->userdata('admin');
			
		if($admin == TRUE)
		{
			$this->load->model('admin_model');
			$oneUseDiscounts = $this->admin_model->getAllOneUseDiscounts();
			$periodDiscounts = $this->admin_model->getAllPeriodDiscounts();
			
			foreach($oneUseDiscounts as & $row)
			{
				if($row['annvand'] == 0)
				{
					$row['annvand'] = "Nej";
				}
				
				else
				{
					$row['annvand'] ="Ja";
				}
			}
			
			$data['title']='Rabatter';
			$data['oneUseDiscounts'] =$oneUseDiscounts;
			$data['periodDiscounts'] =$periodDiscounts;
			$this->load->view('global/view_master_header',$data);
			$this->load->view('admin/view_admin_discounts', $data);
			$this->load->view('global/view_master_footer');	
		}
		else
		{
			show_404();
		}
	}


	
function int($int)
	{
		//kontrollerar om det är en int
        // Kontrollerar s att det är ett numreriskt värde
        if(is_numeric($int) === TRUE){
           
            // det är ett nummer
            if((int)$int == $int){

                return TRUE;
               
            // det är ett nummer men inte en int
            }
			else
			{
           
                return FALSE;
            }
       
        // inte ett nummer.
        }
		else
		{
       
            return FALSE;
        }
    }
	

}

?>